package com.situ.mallbrand.controller;

import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;
@CrossOrigin
@RestController
@RequestMapping("/api/upload")
public class UploadController {
    @PostMapping
    public Map<String,Object> uploadLogo(@RequestParam("file" ) MultipartFile file) throws IOException {
        String fileName = UUID.randomUUID() + "-" + file.getOriginalFilename();
        //2.本地存储路径
        String uploadDir = "E:/uploads/";
        File dir = new File(uploadDir);
        if(!dir.exists()) dir.mkdirs();

        //3.保存文件
        File dest = new File(uploadDir + fileName);
        file.transferTo(dest);

        //4.返回图片可访问url
        String url = "/uploads/" + fileName;
        Map<String,Object> result = new HashMap<>();
        result.put("url",url);
        return result;
    }
}
